from django.db import models
import random, string
# Create your models here.


def createuuid():
    alphabet = "23456789abcdefghijkmnopqrstuvwxyzABCEDFGHJKMNOPQRSTUVWXYZ"
    res = ""
    for i in range(32):
        res += random.choice(alphabet)
    return res


class Base(models.Model):
    """所有数据的基类"""
    uuid = models.CharField('UUID', max_length=32, primary_key=True, default=createuuid, editable=False)
    is_del = models.PositiveIntegerField('是否删除', default=1)
    add_time = models.DateTimeField('创建时间', auto_now_add=True)

    class Meta:
        abstract = True


class User(Base):
    AUTHORITY_LEVEL = ((0, '普通用户'), (1, '管理员'))

    user_name = models.CharField('用户名字', max_length=20, null=False, unique=True)
    pwd = models.CharField('密码', max_length=32, null=False)
    authority = models.IntegerField('权限级别', default=0, choices=AUTHORITY_LEVEL)
    publishable = models.PositiveIntegerField('是否可发布文章', default=1)
    introduction = models.CharField('用户介绍', max_length=80, null=True)
    icon = models.CharField('头像名称', max_length=50)
    nickname = models.CharField('昵称', max_length=30, null=False, unique=True)


class ArticleType(Base):
    type_name = models.CharField('类型名称', max_length=30, unique=True)
    supertype = models.ForeignKey('self', verbose_name="类型", null=True)


class Article(Base):
    SIGNIFICANCE = ((0, '普通'), (1, '置顶'), (2, '公告'))
    STATEMSG = ((0,"草稿"),(1,"文章"))
    title = models.CharField('文章题目', max_length=60, null=False)
    type_id = models.ForeignKey(ArticleType, verbose_name="类型的uuid")
    owner = models.ForeignKey(User, verbose_name="拥有者的uuid")
    level = models.PositiveIntegerField('文章级别', default=0, choices=SIGNIFICANCE)
    content = models.TextField('文章')
    picture_path = models.CharField('图片地址', max_length=50)
    state = models.PositiveIntegerField('文章状态', choices=STATEMSG)